![]() |
PATH![]() |
![]() ![]() |
Every script is a series of statements. Statements are structures similar to sentences in human languages that contain instructions for AppleScript to perform. When AppleScript runs a script, it reads the statements in order and carries out their instructions. Some statements cause AppleScript to skip or repeat certain instructions or change the way it performs certain tasks. These statements, which are described in Chapter 7, are called control statements .
All statements, including control statements, fall into one of two categories: simple statements or compound statements. Simple statements are statements such as the following that are written on a single line.
tell application "Finder" to close the front window
Compound statements are statements that are written on more than one line and contain other statements. All compound statements have two things in common: they can contain any number of statements, and they have the word end (followed, optionally, by the first word of the statement) as their last line. The simple statement above is equivalent to the following compound statement.
tell application "Finder"
close the front window
end tell
The compound Tell statement includes the lines tell application " Finder " and end tell , and all statements between those two lines.
A compound statement can contain any number of statements. For example, here is a Tell statement that contains two statements:
tell application "Finder"
set windowName to name of front window
close front window
end tell
This example illustrates the advantage of using a compound Tell statement: you can add additional statements within a compound statement.
Notice that the previous example contains the statement close front window instead of close the front window . AppleScript allows you to add or remove the word the anywhere in a script without changing the meaning of the script. You can use the word the to make your statements more English-like and therefore more readable.
Here's another example of a compound statement:
if the name of the front window is "Fred" then
close front window
end if
Statements contained in a compound statement can themselves be compound statements. Here's an example:
tell application "Finder"
if the name of the front window is "Fred" then
close front window
end if
end tell